package bai.publicwelfare_backend.mapper.user;

import bai.publicwelfare_backend.pojo.dto.user.ChangeUserDTO;
import bai.publicwelfare_backend.pojo.entity.user.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    // 插入用户并返回主键
    @Insert("INSERT INTO user(username, password) " +
            "VALUES(#{username}, #{password})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(User user);

    // 更新token版本（用于强制下线）
    @Update("UPDATE user SET token_version = token_version + 1 WHERE id = #{userId}")
    void incrementTokenVersion(Long userId);

//    更新密码
    @Update("UPDATE user SET password = #{password} WHERE id = #{userId}")
    void updatePassword(@Param("userId") Long userId, @Param("password") String password);

    // 根据用户名查询
    @Select("SELECT * FROM user WHERE username = #{username}")
    User selectByUsername(String username);

    // 根据用户ID查询
    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectById(Long id);
    // 根据用户id查询版本号
    @Select("SELECT token_version FROM user WHERE id = #{userId}")
    Integer getTokenVersionById(Long userId);
    @Update("UPDATE user SET username = #{userName}, updated_time = NOW() WHERE id = #{id}")
    void setUserName(ChangeUserDTO changeUserDTO);
}
